use serde::{Deserialize, Serialize};
use share::core::native_version::NativeVersion;
use snafu::Snafu;

#[derive(Debug, Snafu)]
pub enum Error {
    #[snafu(display(
        "latest native version can not be smaller than min native version, min: {min}, latest: {latest}"
    ))]
    InvalidNativeVersion {
        min: NativeVersion,
        latest: NativeVersion,
    },
}

type Result<T> = std::result::Result<T, Error>;

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct NativeVersionPair {
    min: NativeVersion,
    latest: NativeVersion,
}

impl NativeVersionPair {
    pub fn new(min: NativeVersion, latest: NativeVersion) -> Result<Self> {
        if latest < min {
            Err(Error::InvalidNativeVersion { min, latest })
        } else {
            Ok(Self { min, latest })
        }
    }

    pub fn min(&self) -> NativeVersion {
        self.min
    }

    pub fn take_min(self) -> NativeVersion {
        self.latest
    }

    pub fn latest(&self) -> NativeVersion {
        self.latest
    }

    pub fn take_latest(self) -> NativeVersion {
        self.latest
    }
}
